package com.wqh.zeromq.util;

import java.io.*;
import java.util.logging.Logger;

public class ObjectAndBytes {

    private static Logger logger = Logger.getLogger(Object.class.getSimpleName());

    public static byte[] objectToBytes(Object obj) {
        logger.info("Object to Bytes........");

        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objStream = new ObjectOutputStream(byteArrayOutputStream);
            //对象输出流写入object
            objStream.writeObject(obj);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Object bytesToObject(byte[] bytes) {
        logger.info("Bytes to Object.......");
        try {

            ByteArrayInputStream byteArrayInputStream =new ByteArrayInputStream(bytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

            return objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
